<!DOCTYPE html>
<html lang="${LANG}" class="dark">

<head>
    <meta charset="UTF-8">
    <title>QBT_TR(Torrent Upload Speed Limiting)QBT_TR[CONTEXT=TransferListWidget]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
    <script src="scripts/lib/MooTools-Core-1.6.0-compat-compressed.js"></script>
    <script src="scripts/lib/MooTools-More-1.6.0-compat-compressed.js"></script>
    <script src="scripts/lib/mocha.min.js"></script>
    <script src="scripts/localpreferences.js?v=${CACHEID}"></script>
    <script src="scripts/color-scheme.js?v=${CACHEID}"></script>
    <script src="scripts/speedslider.js?v=${CACHEID}"></script>
</head>

<body>
    <div style="padding-top: 10px; width: 100%; text-align: center; margin: 0 auto; overflow: hidden">
        <div id="uplimitSlider" class="slider">
            <div id="uplimitUpdate" class="update">
                <label for="uplimitUpdatevalue">QBT_TR(Upload limit:)QBT_TR[CONTEXT=PropertiesWidget]</label>
                <input type="text" id="uplimitUpdatevalue" size="6" placeholder="∞" style="text-align: center;">
                <span id="upLimitUnit">QBT_TR(KiB/s)QBT_TR[CONTEXT=SpeedLimitDialog]</span>
            </div>
            <div class="sliderWrapper">
                <div id="uplimitSliderknob" class="sliderknob"></div>
                <div id="uplimitSliderarea" class="sliderarea"></div>
            </div>
            <div class="clear"></div>
        </div>
        <input type="button" id="applyButton" value="QBT_TR(Apply)QBT_TR[CONTEXT=HttpServer]" onclick="setUpLimit()">
    </div>

    <script>
        "use strict";

        window.addEventListener("keydown", (event) => {
            switch (event.key) {
                case "Enter":
                    event.preventDefault();
                    $("applyButton").click();
                    break;
                case "Escape":
                    event.preventDefault();
                    window.parent.qBittorrent.Client.closeFrameWindow(window);
                    break;
            }
        });

        const hashes = new URLSearchParams(window.location.search).get("hashes").split("|");
        const setUpLimit = () => {
            const limit = Number($("uplimitUpdatevalue").value) * 1024;
            if (hashes[0] === "global") {
                fetch("api/v2/transfer/setUploadLimit", {
                        method: "POST",
                        body: new URLSearchParams({
                            limit: limit
                        })
                    })
                    .then((response) => {
                        if (!response.ok)
                            return;

                        window.parent.updateMainData();
                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                    });
            }
            else {
                fetch("api/v2/torrents/setUploadLimit", {
                        method: "POST",
                        body: new URLSearchParams({
                            hashes: hashes.join("|"),
                            limit: limit
                        })
                    })
                    .then((response) => {
                        if (!response.ok)
                            return;

                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                    });
            }
        };

        $("uplimitUpdatevalue").focus();

        MochaUI.addUpLimitSlider(hashes);
    </script>
</body>

</html>
